﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using ZhouYu.NewWorld.BLL.IService;
using ZhouYu.NewWorld.Model;
using ZhouYu.NewWorld.ViewModel;
using ZhouYu.NewWorld.Web.Models;

namespace ZhouYu.NewWorld.Web.Controllers
{
    public class DeliveryAddressController : Controller
    {
        //注入
        private IDeliveryAddressService _deliveryAddressService;
        public DeliveryAddressController(IDeliveryAddressService deliveryAddressService)
        {
            _deliveryAddressService = deliveryAddressService;
        }

        #region 显示数据
        /// <summary>
        /// 显示数据
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public JsonResult GetModulePageList(int page, int limit, string name)
        {

            PageListResult<DeliveryAddress> resultDataSet = new PageListResult<DeliveryAddress>();
            resultDataSet.code = 0;
            resultDataSet.msg = string.Empty;
            int count = 0;
            Expression<Func<DeliveryAddress, bool>> Wherelambda = c => true;
            
            //模糊查询
            if (!string.IsNullOrEmpty(name))
            {
                Wherelambda = a => a.Delivery_Name.Contains(name);
            }
            Expression<Func<DeliveryAddress, Nullable<int>>> OrderBylambda = c => c.User_Id;
            resultDataSet.data = _deliveryAddressService.QueryPageList(page, limit, Wherelambda, OrderBylambda, out count);
            resultDataSet.count = count;
            return Json(resultDataSet, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        public JsonResult DelMenuId(int id)
        {
            ResponseResult ResponseResult = new ResponseResult();
            ResponseResult.Success = _deliveryAddressService.Deletes(id);
            return Json(ResponseResult);
        }
        #endregion

        #region 添加
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="hotel"></param>
        /// <returns></returns>
        public JsonResult AddDeliveryAddress(DeliveryAddress deliveryAddress)
        {
            ResponseResult ResponseResult = new ResponseResult();
            string name = deliveryAddress.Delivery_Name;
            deliveryAddress.Delivery_UpdateTime = DateTime.Now;
            deliveryAddress.Delivery_CreateTime = DateTime.Now;
            ResponseResult.Success = _deliveryAddressService.Add(deliveryAddress);
            return Json(ResponseResult);
        }
        #endregion

        #region 修改
        /// <summary>
        /// 修改
        /// </summary>
        /// <returns></returns>
        public JsonResult UpdateDeliveryAddress(DeliveryAddress deliveryAddress)
        {
            ResponseResult ResponseResult = new ResponseResult();
            DeliveryAddress delivery=_deliveryAddressService.Query(a => a.Delivery_Id == deliveryAddress.Delivery_Id);
            //DeliveryAddress.User_Id = $("#User_Id").val();
            //DeliveryAddress.Delivery_Zipcode = $("#Delivery_Zipcode").val();
            //DeliveryAddress.Delivery_TelPhone = $("#Delivery_TelPhone").val();
            //DeliveryAddress.Delivery_Name = $("#Delivery_Name").val();
            //DeliveryAddress.Delivery_UpdateTime = $("#Delivery_UpdateTime").val();
            //DeliveryAddress.Delivery_CreateTime = $("#Delivery_CreateTime").val();
            delivery.User_Id = deliveryAddress.User_Id;
            delivery.Delivery_Zipcode = deliveryAddress.Delivery_Zipcode;
            delivery.Delivery_TelPhone = deliveryAddress.Delivery_TelPhone;
            delivery.Delivery_Name = deliveryAddress.Delivery_Name;
            delivery.Delivery_UpdateTime = deliveryAddress.Delivery_UpdateTime;
            delivery.Delivery_CreateTime = deliveryAddress.Delivery_CreateTime;
            delivery.Delivery_CreateTime = DateTime.Now;
            delivery.Delivery_UpdateTime = DateTime.Now;
            ResponseResult.Success = _deliveryAddressService.Update(delivery);
            return Json(ResponseResult);
        }
        #endregion

        #region 批量删除
        /// <summary>
        /// 根据Id 删除多个实体
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public JsonResult BatchDelete(string Id)
        {
            //string转换string[]数据
            string[] strArray = Id.Split(',');
            int[] iNums;
            //string[]转换int[]
            iNums = Array.ConvertAll(strArray, int.Parse);
            ResponseResult ResponseResult = new ResponseResult();
            ResponseResult.Success = _deliveryAddressService.BatchDeleteById(iNums);
            return Json(ResponseResult);
        }
        #endregion

        // GET: DeliveryAddress
        public ActionResult Index()
        {
            return View();
        }
    }
}